package com.xbfinance.portal.service.impl;

import com.xbfinance.portal.dao.P2pProjectMapper;
import com.xbfinance.portal.dao.P2pProjectOrderMapper;
import com.xbfinance.portal.pojo.po.*;
import com.xbfinance.portal.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private P2pProjectMapper p2pProjectMapper;

    @Autowired
    private P2pProjectOrderMapper p2pProjectOrderMapper;

    @Override
    public void addOrder(Long projectid, Integer userid, Double money) {

        try {
            P2pProjectExample example = new P2pProjectExample();

            example.createCriteria().andProidEqualTo(projectid);

            List<P2pProject> p2pProjects = p2pProjectMapper.selectByExample(example);

            P2pProject project = p2pProjects.get(0);

            Double proapr = project.getProapr();
            Integer protimelimit = project.getProtimelimit();
            Double repayaccount = (money*proapr)/(12/protimelimit);
            Double interest = repayaccount-money;

            P2pProjectOrder order = new P2pProjectOrder();

            Date date = new Date();

            order.setUserid(userid);
            order.setProjectid(projectid);
            order.setMoney(money);
            order.setRealmoney(money);
            order.setRepayaccount(repayaccount);
            order.setInterest(interest);
            order.setAddtime(date);

            p2pProjectOrderMapper.insert(order);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<ProjectOrder> queryOrder(Integer userid) {

        List<ProjectOrder> p2pProjectOrders = p2pProjectOrderMapper.selectByUserid(userid);

        return p2pProjectOrders;
    }

    @Override
    public List<ProjectOrder> queryOrderByName(Integer userid, String name) {

        Map<String,Object> map = new HashMap<>();

        map.put("userid",userid);
        map.put("name",name);

        List<ProjectOrder> list = p2pProjectOrderMapper.queryOrderByName(map);

        return null;
    }
}
